﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace EWms.Core.Components;

public static class JsonOptionSetup
{
    /// <summary>
    /// 添加默认 Json 序列化/反序列化 配置
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddJsonOptionSetup(this IServiceCollection services)
    {
        services.AddControllers().AddNewtonsoftJson(options =>
        {
            // 时间格式化
            options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            // 忽略循环引用
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            // 将枚举类型转换为字符串
            options.SerializerSettings.Converters.Add(new StringEnumConverter());
        });

        return services;
    }
}
